package cn.flying.system.web;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.constants.Constants;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.TreeSelectInfo;
import cn.flying.base.core.common.utils.Ft;
import cn.flying.base.security.service.SysUserService;
import cn.flying.base.security.vo.UserPassInfo;
import cn.flying.system.domain.cm.CmPositionUser;
import cn.flying.system.domain.cm.CmUser;
import cn.flying.system.domain.cm.CmUserRole;
import cn.flying.system.service.cm.CmCodeService;
import cn.flying.system.service.cm.CmOrganizationService;
import cn.flying.system.service.cm.CmPositionService;
import cn.flying.system.service.cm.CmRoleService;
import cn.flying.system.service.cm.CmUserService;

/**
 * Created with IntelliJ IDEA.
 *
 * @description: 首页请求处理类
 * @author: lvyong
 * @date: 2019-12-04
 * @time: 11:02 上午
 * @version: 1.0
 */
@Controller
public class IndexController {

    @Resource
    private CmUserService cmUserService;
    @Resource
    private CmCodeService cmCodeService;
    @Resource
    private CmOrganizationService cmOrganizationService;
    @Resource
    private CmPositionService cmPositionService;
    @Resource
    private CmRoleService cmRoleService;
    @Resource
    private SysUserService sysUserService;

    /**
     * 访问首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/index", "/"})
    public String index(Model model) {
        String username = (String) Ft.ThreadLocal.get(Constants.USER_NAME);
        model.addAttribute("username", username);
        return "index";
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("/setting/user/password")
    public String password(Model model) {
        String userId = (String) Ft.ThreadLocal.get(Constants.USER_ID);
        CmUser user = cmUserService.findUserByUserId(userId);
        model.addAttribute("user", user);
        return "setting/user/password";
    }

    /**
     * 设置密码
     * @return
     */
    @RequestMapping("/user/setpass")
    @ResponseBody
    public Rt setPass(@Validated @RequestBody UserPassInfo info) {
        sysUserService.updateUserPassword(info);
        return Rt.success("密码修改成功！");
    }

    /**
     * 当前登录用户基本信息
     *
     * @return
     */
    @RequestMapping("/setting/user/info")
    public String info(Model model) {
        Map<String, String> genderList = cmCodeService.getCodeList("gender");
        model.addAttribute("genderList", genderList);
        List<TreeSelectInfo> treeList = cmOrganizationService.getOrganizationTree();
        model.addAttribute("treeList", treeList);
        String userId = (String) Ft.ThreadLocal.get(Constants.USER_ID);
        CmUser user = cmUserService.findUserByUserId(userId);
        model.addAttribute("user", user);
        List<CmUserRole> roles = cmRoleService.getUserRoleList(user.getUserId());
        if (CollectionUtils.isEmpty(roles)) {
            model.addAttribute("roles", new ArrayList<String>());
        } else {
            model.addAttribute("roles", roles.stream().map(p -> p.getRoleId()).collect(Collectors.toList()));
        }
        List<CmPositionUser> positionUserList = cmPositionService.getPositionUserList(user.getUserId());
        if (CollectionUtils.isEmpty(positionUserList)) {
            model.addAttribute("positions", new ArrayList<String>());
        } else {
            model.addAttribute("positions", positionUserList.stream().map(p -> p.getPostId()).collect(Collectors.toList()));
        }
        return "setting/user/info";
    }

    /**
     * 首页内容版块
     *
     * @return
     */
    @RequestMapping("/home/console")
    public String console() {
        return "home/console";
    }
}
